<template>
  <!-- 添加或修改菜单对话框 -->
  <el-dialog
    :title="!form.id ? '新增' : '修改'"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="form" :rules="rules" label-width="80px">
      <el-row v-if="!form.id">
        <el-col :span="24">
          <el-form-item label="上级机构" prop="parentId">
            <treeselect
              v-model="form.parentId"
              :options="menuOptions"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="选择上级机构"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="编码" prop="name">
            <el-input v-model="form.code" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-col :span="24">
        <el-form-item label="字典编码" prop="dictCode">
          <el-input v-model="form.dictCode" style="width: 100%;" />
        </el-form-item>
      </el-col>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
// import { doGet, doPost, doDelet, doPut } from '@/router/axios'
import { formCreate } from "@/api/index";
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'DictItemTree',
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,

      // 菜单树选项
      menuOptions: [],
      // 是否显示弹出层
      visible: false,
      // 图标
      form: {
        id: '',
        parentId: '1',
        dictId: '',
        dictCode: '',
        name: '',
        code: '',
        lev: ''
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        parentId: [{ required: true, message: '请选择父类', trigger: 'blur' }],
        code: [{ required: true, message: '请输入编码', trigger: 'blur' }],
        dictCode: [{ required: true, message: '请输入字典编码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init(isEdit, id) {
      if (id != null) {
        this.form.parentId = id
      }
      this.visible = true
      this.getTreeselect()
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (isEdit) {
          // doGet('/sys/dictItemTree/' + id).then(response => {
          //   this.form = response.data.data
          // })
           formCreate.dictItemTree(id).then(response => {
            this.form = response.data
          })
        } else {
          this.form.menuId = undefined
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.form.parentId === undefined) {
            this.form.parentId = -1
          }
          if (this.form.id) {
            // doPut('/sys/dictItemTree/update', this.form).then(data => {
            //   this.$message.success('修改成功')
            //   this.visible = false
            //   this.$emit('refreshDataList')
            // })
            formCreate.dictItemTreeUpdate(this.form).then((res)=>{
              this.$message.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            })
          } else {
            // doPost('/sys/dictItemTree/save', this.form).then(res => {
            //   if (res.data.code === 200) {
            //     this.$message.success('添加成功')
            //     this.visible = false
            //     this.$emit('refreshDataList')
            //   } else {
            //     this.$message.error(res.data.msg)
            //     return false
            //   }
            // })
              formCreate.dictItemTreeSave(this.form).then(res => {
              if (res.code === 200) {
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              } else {
                this.$message.error(res.msg)
                return false
              }
            })
          }
        }
      })
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      // doGet('/sys/dictItemTree/selectTree').then(response => {
      //   this.menuOptions = []
      //   const treeData = response.data.data
      //   this.menuOptions.push(treeData)
      // })
       formCreate.dictItemTreeSelectTree().then(response => {
        this.menuOptions = []
        const treeData = response.data
        this.menuOptions.push(treeData)
      })
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    }
  }
}
</script>
